package cn.com.cifi.mars.web.action.sys;


import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.bean.vo.price.NoticePriceCompareVo;
import cn.com.cifi.mars.bean.vo.price.PriceNewestVersion;
import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.*;
import cn.com.cifi.mars.entity.SysNotice;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.service.PriceVersionService;
import cn.com.cifi.mars.service.PriceVersionValueOrgService;
import cn.com.cifi.mars.service.SysLogService;
import cn.com.cifi.mars.service.SysNoticeService;
import cn.com.cifi.mars.util.WebUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  系统待办前端控制器
 * </p>
 *
 * @author yyd
 * @since 2019-10-16
 */
@RestController
public class SysNoticeController extends BaseAction implements ISysNoticeController{

	@Autowired
	private SysNoticeService sysNoticeService;

	@Autowired
	private PriceVersionService priceVersionService;
	
	@Autowired
	private SysLogService sysLogService;

	@Autowired
	private PriceVersionValueOrgService priceVersionValueOrgService;

	/**
	 * 系统待办分页列表
	 */
	@Override
	public R<String> getSysNoticePage(String current, String pageSize) {
		return ok(sysNoticeService.getSysNoticePage(current,pageSize));
	}

	/**
	 * 手动调价
	 */
	@Override
	public R<PriceNewestVersion> updateManualPrice(String apiVersion, String noticeId) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		IdmUser user = getOperator();
		SysLogBO bo=new SysLogBO();
		bo.setEvent(LogEventEnum.ADD);
		bo.setSource(LogSrcEnum.PC);
		bo.setIp(WebUtils.getIpAddr(request));
		bo.setUrl(WebUtils.getUrl(request));
		bo.setModule("系统待办");
		bo.setTitle("手动调价");
		bo.setContent("手动调价");
		sysLogService.add(bo, user.getAlias());
		return ok(sysNoticeService.updateManualPrice(noticeId, user));
	}

	/**
	 * 版本对比
	 */
	@Override
	public R<NoticePriceCompareVo> noticeAgencyInfoCompareTo(String apiVersion, String noticeId) {

		SysNotice notice = sysNoticeService.selectById(noticeId);
		if (notice != null){
			String taskSource = notice.getTaskSource();

			if (StringUtils.isNotEmpty(taskSource) && taskSource.equals(VersionTypeEunm.INVESTMENT.getKey())){
				//sysNoticeService.panoCastDecisionVersionChange(notice.getStagesId())
				return ok();
			}else if (StringUtils.isNotEmpty(taskSource)){
				return ok(priceVersionValueOrgService.dynamicVersionCompare(notice.getStagesId(), taskSource));
				//return ok();
			}
		}else {
			throw new MsgException(CodeEnum.SYS_NOTICE_ERROR);
		}

		return null;
	}

	/**
	 * 根据分期id获取最新的待办id,为空表示不存在待办
	 */
	@Override
	public R<List<SysNotice>> getSysNoticeByStageId(String apiVersion, String stagesId) {
		return ok(sysNoticeService.getNoticeId(stagesId));
	}

	@Override
	public R<String> noticeCheckSupplementPortalTodo(String apiVersion) {
		sysNoticeService.noticeCheckSupplementPortalTodo();
		return ok();
	}

}
